<?php

namespace App\Console\Commands;

use InvalidArgumentException;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class ServiceMakeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:service {ServiceName}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new service class which will use to be registed by service provider';


    /**
     * The Filesystem instance
     *
     * @var Filesystem
     */
    protected $files;


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(Filesystem $files)
    {
        parent::__construct();

        $this->files = $files;
    }


    protected function getStub()
    {
        return __DIR__.'/stubs/Service.plain.stub';
    }


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $serviceName = $this->arguments()['ServiceName'];

        $serviceName = $this->parseService($serviceName);
         
        $namespace = $this->getNamespace($serviceName);
        
        $this->makePath($serviceName); 
        //$content = $this->files->get($this->getStub()); 
         
        //$this->replace($content, $replace);

        
       
        var_dump($serviceName);exit;
    }
    
    /**
     * Parse service name 
     *
     * @param string $service
     * @return string
     */
    protected function parseService($service)
    {
        if (preg_match('([^A-Za-z0-9_/\\\\])',$service)) {
            throw new InvalidArgumentException('Service name contains invalid characters');
        } 

        $service = trim(str_replace('/','\\',$service), '\\');

        return $service;
    }
    
    /**
     * Replace all dummies in input
     * 
     * @param string $input
     * @param array $replace
     * @return $this
     */
/*    protected function replace(&$input, $replace)
    {
        $find = array_keys($replace);

        $replace = array_values($replace);
 
        $input = str_replace($find, $replace, $input);

        return $this;   
    }
*/

    /**
     * Generate the file of service
     *
     *
     */
    protected function makeService($path, $input)
    {
        $this->files->put($path,$input);
    }

    /**
     *
     *
     */
    protected function makePath($name)
    { 
        if(preg_match('#([A-Za-z0-9_/\\\\]*?)([^/\\\\]+)$#',$name,$match)) {
            $path = $this->laravel['path'].'/Service'.(empty($match[1])? '':('/'.trim($match[1],'\\')));
            $path = str_replace('\\','/',$path);

            mkdir($path,0744,true);
        }
        return $path;
    }


    protected function getNamespace($name)
    {
        if(preg_match('#([A-Za-z0-9_/\\\\]*?)([^/\\\\]+)$#',$name,$match)) {
            $namespace = 'App\\Service'.(empty($match[1])? ';':('\\'.trim($match[1],'\\').';'));
        }

        return $namespace;
    }


    protected function getClassname($name)
    {
        if(preg_match('#([A-Za-z0-9_/\\\\]*?)([^/\\\\]+)$#',$name,$match)) {
            $class = $match[2];
        }

        return $classname;
    }


    
}



















